package com.hefu.virtuallife.ui;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.text.Layout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hefu.virtuallife.R;
import com.hefu.virtuallife.bean.SmartHomeBean;
import com.hefu.virtuallife.constant.AppValue;
import com.hefu.virtuallife.ui.adapter.SmartEqAdapter;
import com.hefu.virtuallife.utils.SmartDataUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SmartSceneSettingsActivity extends AppCompatActivity implements View.OnClickListener{

    @BindView(R.id.lv_smart_eq)
    ListView lv_smart_eq;
    @BindView(R.id.img_back)
    ImageView back_btn;
    @BindView(R.id.tv_save)
    TextView tv_save;
    @BindView(R.id.iv_edit_name)
    ImageView iv_edit_name;
    @BindView(R.id.iv_select_pic)
    ImageView iv_select_pic;
    @BindView(R.id.iv_scene)
    ImageView iv_scene;
    @BindView(R.id.tv_sceneName)
    TextView tv_sceneName;


    private Context mContext;
    private Dialog mPicDialog;
    private List<SmartHomeBean> list_eq;
    private String sceneName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smart_scene_settings);

        mContext = this;
        ButterKnife.bind(this);
        String scene = getIntent().getStringExtra("scene");
        sceneName = scene;
        int imgId = getIntent().getIntExtra("imgId", R.drawable.scene_memorialday_bg);

        tv_sceneName.setText(sceneName);
        iv_scene.setImageResource(imgId);
        if (null != scene){
            list_eq = SmartDataUtils.querySceneEquip(scene);
            if (null == list_eq || list_eq.size() == 0){
                return;
            }
            SmartEqAdapter adapter = new SmartEqAdapter(this, list_eq);
            lv_smart_eq.setAdapter(adapter);
        }

        back_btn.setOnClickListener(this);
        tv_save.setOnClickListener(this);
        iv_edit_name.setOnClickListener(this);
        iv_select_pic.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_save:
                SmartDataUtils.update(list_eq);
                Toast.makeText(this, "保存成功",Toast.LENGTH_LONG).show();
                finish();
                break;
            case R.id.iv_select_pic:
                showSelectPicDialog();
                break;
            case R.id.iv_edit_name:
                showInputDialog();
                break;
            case R.id.bt_take_pic:
                mPicDialog.dismiss();
                File outputImage = new File(mContext.getExternalCacheDir(), "output_image.jpg");
                try {
                    if (outputImage.exists()) {
                        outputImage.delete();
                    }
                    outputImage.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                Uri imageUri;
                String s = this.getClass().getName();
                if (Build.VERSION.SDK_INT >= 24) {
                    imageUri = FileProvider.getUriForFile(mContext, "com.hefu.virtuallife.fileprovider", outputImage);
                } else {
                    imageUri = Uri.fromFile(outputImage);
                }
                //启动相机程序
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(intent, AppValue.REQUEST_CODE_IMAGE_FROM_CAMERA);
                break;
            case R.id.bt_photo:
                mPicDialog.dismiss();
                startActivityForResult(new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI),
                        AppValue.REQUEST_CODE_IMAGE_FROM_FILE);
                break;
            case R.id.bt_cancel:
                mPicDialog.dismiss();
                break;
            default:
                break;
        }
    }

    private void showInputDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("请输入场景名称");    //设置对话框标题

        final EditText edit = new EditText(mContext);
        edit.setLines(3);
        edit.setPadding(30, 0 ,30, 0);

        builder.setView(edit);
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                sceneName = edit.getText().toString();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });

        builder.setCancelable(true);    //设置按钮是否可以按返回键取消,false则不可以取消
        AlertDialog dialog = builder.create();  //创建对话框
        dialog.setCanceledOnTouchOutside(true); //设置弹出框失去焦点是否隐藏,即点击屏蔽其它地方是否隐藏
        dialog.show();
    }

    private void showSelectPicDialog(){
        mPicDialog = new Dialog(this, R.style.bottomDialogStyle);
        //填充对话框的布局
        View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_bottom, null);
        //初始化控件
        inflate.findViewById(R.id.bt_take_pic).setOnClickListener(this);
        inflate.findViewById(R.id.bt_photo).setOnClickListener(this);
        inflate.findViewById(R.id.bt_cancel).setOnClickListener(this);
        //将布局设置给Dialog
        mPicDialog.setContentView(inflate);
        //获取当前Activity所在的窗体
        Window dialogWindow = mPicDialog.getWindow();
        if(dialogWindow == null){
            return;
        }
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        dialogWindow.setLayout(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
        //获得窗体的属性
//        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//        lp.y = 20;//设置Dialog距离底部的距离
//        //将属性设置给窗体
//        dialogWindow.setAttributes(lp);
        mPicDialog.show();//显示对话框
    }
}
